<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="1280" class="contract-form-dialog">
    <div class="dialog-content">
      <!-- 头部区域 -->
      <div class="dialog-header">
        <div class="header-icon">
          <Icon icon="ep:document" />
        </div>
        <div class="header-content">
          <h3 class="dialog-title">{{ dialogTitle }}</h3>
          <p class="dialog-subtitle">配置合同基本信息和产品清单</p>
        </div>
      </div>

      <!-- 表单区域 -->
      <div class="form-container">
        <el-form
          ref="formRef"
          v-loading="formLoading"
          :model="formData"
          :rules="formRules"
          label-width="120px"
          class="enhanced-form"
        >
          <!-- 基本信息 -->
          <div class="form-section">
            <div class="section-header">
              <Icon icon="ep:document" class="section-icon" />
              <span class="section-title">基本信息</span>
            </div>

            <div class="form-grid">
              <el-form-item label="合同编号" prop="no" class="form-item">
                <div class="input-wrapper">
                  <Icon icon="ep:document-copy" class="input-icon" />
                  <el-input
                    disabled
                    v-model="formData.no"
                    placeholder="保存时自动生成"
                    class="enhanced-input disabled-input"
                  />
                </div>
              </el-form-item>

              <el-form-item label="合同名称" prop="name" class="form-item">
                <div class="input-wrapper">
                  <Icon icon="ep:document" class="input-icon" />
                  <el-input
                    v-model="formData.name"
                    placeholder="请输入合同名称"
                    class="enhanced-input"
                  />
                </div>
              </el-form-item>

              <el-form-item label="负责人" prop="ownerUserId" class="form-item">
                <div class="select-wrapper">
                  <Icon icon="ep:user" class="select-icon" />
                  <el-select
                    filterable
                    v-model="formData.ownerUserId"
                    :disabled="formType !== 'create'"
                    class="enhanced-select"
                    placeholder="请选择负责人"
                  >
                    <el-option
                      v-for="item in userOptions"
                      :key="item.id"
                      :label="item.nickname"
                      :value="item.id"
                    />
                  </el-select>
                </div>
              </el-form-item>
            </div>
          </div>

          <!-- 客户和商机信息 -->
          <div class="form-section">
            <div class="section-header">
              <Icon icon="ep:office-building" class="section-icon" />
              <span class="section-title">客户和商机信息</span>
            </div>

            <div class="form-grid">
              <el-form-item label="客户名称" prop="customerId" class="form-item">
                <div class="select-wrapper">
                  <el-select-v2
                    v-model="formData.customerId"
                    :options="customerList"
                    :props="{
                      value: 'id',
                      label: 'name'
                    }"
                    :disabled="formData.customerDefault"
                    filterable
                    clearable
                    placeholder="请选择客户"
                    class="enhanced-select-v2"
                    @change="handleCustomerChange"
                  />
                </div>
              </el-form-item>

              <el-form-item label="商机名称" prop="businessId" class="form-item">
                <div class="select-wrapper">
                  <Icon icon="ep:briefcase" class="select-icon" />
                  <el-select
                    @change="handleBusinessChange"
                    :disabled="!formData.customerId"
                    v-model="formData.businessId"
                    class="enhanced-select"
                    placeholder="请选择商机"
                  >
                    <el-option
                      v-for="item in getBusinessOptions"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id!"
                    />
                  </el-select>
                </div>
              </el-form-item>
            </div>
          </div>

          <!-- 时间信息 -->
          <div class="form-section">
            <div class="section-header">
              <Icon icon="ep:calendar" class="section-icon" />
              <span class="section-title">时间信息</span>
            </div>

            <div class="form-grid">
              <el-form-item label="下单日期" prop="orderDate" class="form-item">
                <div class="date-wrapper">
                  <Icon icon="ep:calendar" class="date-icon" />
                  <el-date-picker
                    v-model="formData.orderDate"
                    placeholder="选择下单日期"
                    type="date"
                    value-format="x"
                    class="enhanced-date-picker"
                  />
                </div>
              </el-form-item>

              <el-form-item label="开始时间" prop="startTime" class="form-item">
                <div class="date-wrapper">
                  <Icon icon="ep:calendar" class="date-icon" />
                  <el-date-picker
                    v-model="formData.startTime"
                    placeholder="选择开始时间"
                    type="date"
                    value-format="x"
                    class="enhanced-date-picker"
                  />
                </div>
              </el-form-item>

              <el-form-item label="结束时间" prop="endTime" class="form-item">
                <div class="date-wrapper">
                  <Icon icon="ep:calendar" class="date-icon" />
                  <el-date-picker
                    v-model="formData.endTime"
                    placeholder="选择结束时间"
                    type="date"
                    value-format="x"
                    class="enhanced-date-picker"
                  />
                </div>
              </el-form-item>
            </div>
          </div>

          <!-- 签约信息 -->
          <div class="form-section">
            <div class="section-header">
              <Icon icon="ep:user-filled" class="section-icon" />
              <span class="section-title">签约信息</span>
            </div>

            <div class="form-grid">
              <el-form-item label="公司签约人" prop="signUserId" class="form-item">
                <div class="select-wrapper">
                  <Icon icon="ep:user-filled" class="select-icon" />
                  <el-select
                    v-model="formData.signUserId"
                    class="enhanced-select"
                    placeholder="请选择公司签约人"
                  >
                    <el-option
                      v-for="item in userOptions"
                      :key="item.id"
                      :label="item.nickname"
                      :value="item.id!"
                    />
                  </el-select>
                </div>
              </el-form-item>

              <el-form-item label="客户签约人" prop="signContactId" class="form-item">
                <div class="select-wrapper">
                  <Icon icon="ep:user" class="select-icon" />
                  <el-select
                    v-model="formData.signContactId"
                    :disabled="!formData.customerId"
                    class="enhanced-select"
                    placeholder="请选择客户签约人"
                  >
                    <el-option
                      v-for="item in getContactOptions"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </div>
              </el-form-item>

              <el-form-item label="备注" prop="remark" class="form-item">
                <div class="textarea-wrapper">
                  <Icon icon="ep:document" class="textarea-icon" />
                  <el-input
                    v-model="formData.remark"
                    placeholder="请输入备注"
                    type="textarea"
                    class="enhanced-textarea"
                    :rows="3"
                  />
                </div>
              </el-form-item>
            </div>
          </div>

          <!-- 产品清单 -->
          <div class="form-section product-section">
            <div class="section-header">
              <Icon icon="ep:goods" class="section-icon" />
              <span class="section-title">产品清单</span>
            </div>

            <ContentWrap class="product-content">
              <el-tabs v-model="subTabsName" class="enhanced-tabs">
                <el-tab-pane label="产品清单" name="product">
                  <div class="product-form-wrapper">
                    <ContractProductForm
                      ref="productFormRef"
                      :products="formData.products"
                      :disabled="disabled"
                    />
                  </div>
                </el-tab-pane>
              </el-tabs>
            </ContentWrap>
          </div>

          <!-- 价格信息 -->
          <div class="form-section price-section">
            <div class="section-header">
              <Icon icon="ep:money" class="section-icon" />
              <span class="section-title">价格信息</span>
            </div>

            <div class="form-grid">
              <el-form-item label="产品总金额" prop="totalProductPrice" class="form-item">
                <div class="price-wrapper">
                  <Icon icon="ep:money" class="price-icon" />
                  <el-input
                    disabled
                    v-model="formData.totalProductPrice"
                    :formatter="erpPriceTableColumnFormattere"
                    class="enhanced-input price-input"
                    placeholder="0.00"
                  />
                  <span class="price-unit">元</span>
                </div>
              </el-form-item>

              <el-form-item label="整单折扣（%）" prop="discountPercent" class="form-item">
                <div class="number-wrapper">
                  <Icon icon="ep:discount" class="number-icon" />
                  <el-input-number
                    v-model="formData.discountPercent"
                    placeholder="请输入整单折扣"
                    controls-position="right"
                    :min="0"
                    :precision="2"
                    class="enhanced-number"
                  />
                </div>
              </el-form-item>

              <el-form-item label="折扣后金额" prop="totalPrice" class="form-item">
                <div class="price-wrapper">
                  <Icon icon="ep:money" class="price-icon" />
                  <el-input
                    disabled
                    v-model="formData.totalPrice"
                    placeholder="0.00"
                    :formatter="erpPriceTableColumnFormattere"
                    class="enhanced-input price-input final-price"
                  />
                  <span class="price-unit">元</span>
                </div>
              </el-form-item>
            </div>
          </div>
        </el-form>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false" class="cancel-btn">
          <Icon icon="ep:close" class="btn-icon" />
          取消
        </el-button>
        <el-button :disabled="formLoading" type="primary" @click="submitForm" class="confirm-btn">
          <Icon icon="ep:check" class="btn-icon" />
          保存
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import * as CustomerApi from '@/api/crm/customer'
import * as ContractApi from '@/api/crm/contract'
import * as UserApi from '@/api/system/user'
import * as ContactApi from '@/api/crm/contact'
import * as BusinessApi from '@/api/crm/business'
import { erpPriceMultiply, erpPriceTableColumnFormatter } from '@/utils'
import { useUserStore } from '@/store/modules/user'
import ContractProductForm from '@/views/crm/contract/components/ContractProductForm.vue'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  no: undefined,
  name: undefined,
  customerId: undefined,
  businessId: undefined,
  orderDate: undefined,
  startTime: undefined,
  endTime: undefined,
  signUserId: undefined,
  signContactId: undefined,
  ownerUserId: undefined,
  discountPercent: 0,
  totalProductPrice: 0,
  remark: undefined,
  products: [],
  customerDefault: false
})
const formRules = reactive({
  name: [{ required: true, message: '合同名称不能为空', trigger: 'blur' }],
  customerId: [{ required: true, message: '客户不能为空', trigger: 'blur' }],
  orderDate: [{ required: true, message: '下单日期不能为空', trigger: 'blur' }],
  ownerUserId: [{ required: true, message: '负责人不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表
const customerList = ref([]) // 客户列表的数据
const businessList = ref<BusinessApi.BusinessVO[]>([])
const contactList = ref<ContactApi.ContactVO[]>([])

/** 子表的表单 */
const subTabsName = ref('product')
const productFormRef = ref()

/** 计算 discountPrice、totalPrice 价格 */
watch(
  () => formData.value,
  (val) => {
    if (!val) {
      return
    }

    const totalProductPrice = val.products.reduce((prev, curr) => prev + curr.totalPrice, 0)
    const discountPrice =
      val.discountPercent != null
        ? erpPriceMultiply(totalProductPrice, val.discountPercent / 100.0)
        : 0
    const totalPrice = totalProductPrice - discountPrice
    // 赋值
    formData.value.totalProductPrice = totalProductPrice.toFixed(2)
    formData.value.totalPrice = totalPrice.toFixed(2)
  },
  { deep: true }
)

/** 打开弹窗 */
const open = async (type: string, id?: number, businessNameId?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 获得客户列表
  customerList.value = await CustomerApi.getCustomerSimpleList()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ContractApi.getContract(id)
    } finally {
      formLoading.value = false
    }
  }

  // 获得用户列表
  userOptions.value = await UserApi.getSimpleUserList()
  // 默认新建时选中自己
  if (formType.value === 'create') {
    formData.value.ownerUserId = useUserStore().getUser.id
    if (businessNameId) {
      formData.value.customerId = businessNameId
      formData.value.customerDefault = true // 默认客户的选择，不允许变
    }
  }
  // 获取联系人
  contactList.value = await ContactApi.getSimpleContactList()
  // 获得商机列表
  businessList.value = await BusinessApi.getSimpleBusinessList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  productFormRef.value.validate()
  try {
    const data = unref(formData.value) as unknown as ContractApi.ContractVO
    if (formType.value === 'create') {
      await ContractApi.createContract(data)
      message.success(t('common.createSuccess'))
    } else {
      await ContractApi.updateContract(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    no: undefined,
    name: undefined,
    customerId: undefined,
    businessId: undefined,
    orderDate: undefined,
    startTime: undefined,
    endTime: undefined,
    signUserId: undefined,
    signContactId: undefined,
    ownerUserId: undefined,
    discountPercent: 0,
    totalProductPrice: undefined,
    remark: undefined,
    products: []
  }
  formRef.value?.resetFields()
}

/** 处理切换客户 */
const handleCustomerChange = () => {
  formData.value.businessId = undefined
  formData.value.signContactId = undefined
  formData.value.products = []
}

/** 处理商机变化 */
const handleBusinessChange = async (businessId: number) => {
  const business = await BusinessApi.getBusiness(businessId)
  business.products.forEach((item) => {
    item.contractPrice = item.businessPrice
  })
  formData.value.products = business.products
}

/** 动态获取客户联系人 */
const getContactOptions = computed(() =>
  contactList.value.filter((item) => item.customerId == formData.value.customerId)
)
/** 动态获取商机 */
const getBusinessOptions = computed(() =>
  businessList.value.filter((item) => item.customerId == formData.value.customerId)
)
</script>

<style lang="scss" scoped>
.contract-form-dialog {
  :deep(.el-dialog) {
    border-radius: 12px;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 20px auto;
      max-height: 90vh;
    }
  }

  :deep(.el-dialog__header) {
    display: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
    max-height: 80vh;
    overflow-y: auto;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: 1px solid #e5e7eb;
  }
}

.dialog-content {
  background: #f8fafc;
}

/* 对话框头部 */
.dialog-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px 24px;
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  color: white;
  border-radius: 10px;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;

  @media (max-width: 768px) {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}

.header-content {
  flex: 1;
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 表单容器 */
.form-container {
  background: white;
  padding: 24px;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-form {
  :deep(.el-form-item__label) {
    font-weight: 500;
    color: #374151;
    font-size: 14px;

    @media (max-width: 768px) {
      font-size: 13px;
    }
  }
}

/* 表单区域 */
.form-section {
  margin-bottom: 24px;
  padding: 20px;
  background: #f8fafc;
  border-radius: 8px;
  border: 1px solid #e5e7eb;

  @media (max-width: 768px) {
    margin-bottom: 16px;
    padding: 16px;
  }

  &.product-section {
    background: white;
    border: 2px solid #e5e7eb;
  }

  &.price-section {
    background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
    border: 2px solid #0ea5e9;
  }
}

.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 2px solid #0052cc;
}

.section-icon {
  color: #0052cc;
  font-size: 16px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #374151;
}

/* 表单网格 */
.form-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;

  @media (max-width: 1200px) {
    grid-template-columns: repeat(2, 1fr);
  }

  @media (max-width: 768px) {
    grid-template-columns: 1fr;
    gap: 16px;
  }
}

.form-item {
  margin-bottom: 0;
}

/* 输入框样式 */
.input-wrapper,
.select-wrapper,
.date-wrapper,
.textarea-wrapper,
.price-wrapper,
.number-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.input-icon,
.select-icon,
.date-icon,
.textarea-icon,
.price-icon,
.number-icon {
  position: absolute;
  left: 12px;
  color: #6b7280;
  font-size: 16px;
  z-index: 1;
}

.textarea-icon {
  top: 12px;
}

.enhanced-input,
.enhanced-select,
.enhanced-select-v2,
.enhanced-date-picker,
.enhanced-textarea,
.enhanced-number {
  width: 100%;

  :deep(.el-input__wrapper) {
    padding-left: 40px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    min-height: 40px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }

  :deep(.el-input__inner) {
    font-size: 14px;

    @media (max-width: 768px) {
      font-size: 16px; // 防止iOS缩放
    }
  }

  :deep(.el-input__suffix) {
    right: 12px;
  }
}

.enhanced-textarea {
  :deep(.el-textarea__inner) {
    padding-left: 40px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    resize: vertical;
    min-height: 80px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &:focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

/* 禁用输入框样式 */
.disabled-input {
  :deep(.el-input__wrapper) {
    background: #f3f4f6;
    border-color: #e5e7eb;

    .el-input__inner {
      color: #9ca3af;
      cursor: not-allowed;
    }
  }
}

/* 价格输入框 */
.price-wrapper {
  .price-unit {
    position: absolute;
    right: 12px;
    color: #6b7280;
    font-size: 14px;
    font-weight: 500;
    z-index: 1;
  }

  .price-input {
    :deep(.el-input__wrapper) {
      padding-right: 40px;
    }

    &.final-price {
      :deep(.el-input__wrapper) {
        background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
        border: 2px solid #f59e0b;
        font-weight: 600;
      }

      :deep(.el-input__inner) {
        color: #92400e;
        font-weight: 600;
        font-size: 16px;
      }
    }
  }
}

/* 产品清单 */
.product-content {
  background: transparent;
  border: none;
  box-shadow: none;
}

.enhanced-tabs {
  :deep(.el-tabs__header) {
    margin-bottom: 16px;
  }

  :deep(.el-tabs__nav-wrap) {
    background: #f8fafc;
    border-radius: 8px;
    padding: 4px;
  }

  :deep(.el-tabs__item) {
    border-radius: 6px;
    font-weight: 500;
    transition: all 0.2s ease;

    &.is-active {
      background: white;
      color: #0052cc;
      box-shadow: 0 2px 8px rgba(0, 82, 204, 0.15);
    }
  }

  :deep(.el-tabs__active-bar) {
    display: none;
  }
}

.product-form-wrapper {
  padding: 16px;
  background: #f8fafc;
  border-radius: 8px;
  border: 1px solid #e5e7eb;
}

/* 对话框底部 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  background: white;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    gap: 8px;
  }
}

.cancel-btn {
  border: 2px solid #e5e7eb;
  color: #6b7280;
  background: white;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.2s ease;
  min-height: 40px;

  &:hover {
    border-color: #d1d5db;
    color: #374151;
  }

  @media (max-width: 768px) {
    width: 100%;
    order: 2;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.confirm-btn {
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  border: none;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  color: white;
  transition: all 0.2s ease;
  min-height: 40px;

  &:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
  }

  @media (max-width: 768px) {
    width: 100%;
    order: 1;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(4px);
}

:deep(.el-loading-spinner) {
  .circular {
    width: 32px;
    height: 32px;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .dialog-content {
    background: #1f2937;
  }

  .form-container {
    background: #374151;
  }

  .form-section {
    background: #4b5563;
    border-color: #6b7280;

    &.product-section {
      background: #374151;
      border-color: #6b7280;
    }

    &.price-section {
      background: linear-gradient(135deg, #1e3a8a 0%, #1e40af 100%);
      border-color: #3b82f6;
    }
  }

  .section-title {
    color: #f9fafb;
  }

  .section-header {
    border-color: #60a5fa;
  }

  .section-icon {
    color: #60a5fa;
  }

  .enhanced-input,
  .enhanced-select,
  .enhanced-select-v2,
  .enhanced-date-picker,
  .enhanced-number {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .enhanced-textarea {
    :deep(.el-textarea__inner) {
      background: #4b5563;
      border-color: #6b7280;
      color: #f9fafb;
    }
  }

  .disabled-input {
    :deep(.el-input__wrapper) {
      background: #374151;
      border-color: #4b5563;

      .el-input__inner {
        color: #6b7280;
      }
    }
  }

  .input-icon,
  .select-icon,
  .date-icon,
  .textarea-icon,
  .price-icon,
  .number-icon {
    color: #d1d5db;
  }

  .price-unit {
    color: #d1d5db;
  }

  .price-input {
    &.final-price {
      :deep(.el-input__wrapper) {
        background: linear-gradient(135deg, #92400e 0%, #b45309 100%);
        border-color: #f59e0b;
      }

      :deep(.el-input__inner) {
        color: #fbbf24;
      }
    }
  }

  .product-form-wrapper {
    background: #4b5563;
    border-color: #6b7280;
  }

  .enhanced-tabs {
    :deep(.el-tabs__nav-wrap) {
      background: #4b5563;
    }

    :deep(.el-tabs__item) {
      color: #d1d5db;

      &.is-active {
        background: #374151;
        color: #60a5fa;
      }
    }
  }

  .dialog-footer {
    background: #374151;
  }

  .enhanced-form {
    :deep(.el-form-item__label) {
      color: #f9fafb;
    }
  }

  :deep(.el-loading-mask) {
    background: rgba(55, 65, 81, 0.9);
  }
}

/* 高对比度模式适配 */
@media (prefers-contrast: high) {
  .enhanced-input,
  .enhanced-select,
  .enhanced-select-v2,
  .enhanced-date-picker,
  .enhanced-number {
    :deep(.el-input__wrapper) {
      border: 2px solid #0052cc;
      box-shadow: none;

      &:hover,
      &.is-focus {
        border-color: #1d4ed8;
        box-shadow: 0 0 0 2px rgba(0, 82, 204, 0.2);
      }
    }
  }

  .enhanced-textarea {
    :deep(.el-textarea__inner) {
      border: 2px solid #0052cc;
      box-shadow: none;

      &:hover,
      &:focus {
        border-color: #1d4ed8;
        box-shadow: 0 0 0 2px rgba(0, 82, 204, 0.2);
      }
    }
  }

  .cancel-btn {
    border: 2px solid #6b7280;
  }

  .confirm-btn {
    background: #0052cc;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  }
}

/* 减少动画模式适配 */
@media (prefers-reduced-motion: reduce) {
  .enhanced-input,
  .enhanced-select,
  .enhanced-select-v2,
  .enhanced-date-picker,
  .enhanced-textarea,
  .enhanced-number,
  .cancel-btn,
  .confirm-btn {
    transition: none;
  }

  .confirm-btn {
    &:hover {
      transform: none;
    }
  }
}

/* 触摸设备优化 */
@media (hover: none) and (pointer: coarse) {
  .enhanced-input,
  .enhanced-select,
  .enhanced-select-v2,
  .enhanced-date-picker,
  .enhanced-number {
    :deep(.el-input__wrapper) {
      min-height: 44px;
    }
  }

  .cancel-btn,
  .confirm-btn {
    min-height: 44px;
    padding: 12px 20px;
  }
}

/* 超小屏幕适配 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 12px;
  }

  .form-container {
    padding: 12px;
  }

  .form-section {
    padding: 12px;
    margin-bottom: 12px;
  }

  .dialog-footer {
    padding: 12px;
  }

  .form-grid {
    gap: 12px;
  }

  .product-form-wrapper {
    padding: 12px;
  }
}

/* 横屏模式适配 */
@media (max-width: 768px) and (orientation: landscape) {
  .dialog-header {
    padding: 12px 16px;
    flex-direction: row;
    text-align: left;
  }

  .form-container {
    padding: 16px;
  }

  .form-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }
}

/* 滚动条样式 */
.scroller {
  height: 200px;
  overflow-y: auto;

  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: #f1f5f9;
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 3px;

    &:hover {
      background: #94a3b8;
    }
  }
}
</style>
